<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta id="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta id="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/erp/js/html5.js"></script>
    <script type="text/javascript" src="/erp/js/respond.min.js"></script>
    <script type="text/javascript" src="/erp/js/PIE_IE678.js"></script>
    <![endif]-->
    <script src="/erp/assets/js/typeahead-bs2.min.js"></script>
    <link href="/erp/assets/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/erp/css/style.css"/>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css"/>
    <script src="/plugins/layui/layui.all.js"></script>
    <link href="/erp/assets/css/codemirror.css" rel="stylesheet">
    <link rel="stylesheet" href="/erp/assets/css/ace.min.css"/>
    <link rel="stylesheet" href="/erp/Widget/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="/erp/assets/css/font-awesome.min.css"/>
    <!--[if IE 7]>
    <link rel="stylesheet" href="/erp/assets/css/font-awesome-ie7.min.css"/>
    <![endif]-->
    <link href="/erp/Widget/icheck/icheck.css" rel="stylesheet" type="text/css"/>
    <link href="/erp/Widget/webuploader/0.1.5/webuploader.css" rel="stylesheet" type="text/css"/>


    <script type="text/javascript" src="/erp/Widget/ueditor/1.4.3/ueditor.config.js"></script>
    <script type="text/javascript" src="/erp/Widget/ueditor/1.4.3/ueditor.all.min.js"></script>
    <script type="text/javascript" src="/erp/Widget/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>

    <title>添加商品</title>
</head>
<body>
<div class="page_right_style">

    <form class="form form-horizontal" id="form-article-add">

        <div class=" clearfix cl">
            <div class="Add_p_s">
                <label class="form-label col-2">商品名称：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="商品名称"
                                                       id="productName"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">商品编号：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="商品编号"
                                                       id="id"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">商品型号：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="商品型号"
                                                       id="productModel"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">产&nbsp;&nbsp;&nbsp;&nbsp;地：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="产地"
                                                       id="producingArea"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">材&nbsp;&nbsp;&nbsp;&nbsp;质：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="材质"
                                                       id="materialQuality"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">品&nbsp;&nbsp;&nbsp;&nbsp;牌：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="品牌" id="brand"
                ></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">商品质量：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="商品质量"
                                                       id="productQuality"
                >kg
                </div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">一级分类：</label>
                <div class="formControls col-2"><span class="select-box">
               <select id="sort" class="select" name="sortId">
                <option value="0">请选择</option>
               </select></span></div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">二级分类：</label>
                <div class="formControls col-2"><span class="select-box">
                <select id="sorts" class="select" name="sortsId">
                <option value="0">请选择</option>
            </select></span>
                </div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">三级分类：</label>
                <div class="formControls col-2"><span class="select-box">
                <select id="sortss" class="select" name="sortssId">
                <option value="0">请选择</option>
            </select></span>
                </div>
            </div>

            <div class="Add_p_s">
                <label class="form-label col-2">商品种类：</label>

                <div class="formControls col-2"><span class="select-box">
				<select id="productType" class="select" onChange="change(this.value)">
                    <option value="0">请选择商品种类</option>
                    <option value="1">普通商品</option>
                    <option value="2">积分商品</option>
                </select>
				</span></div>
            </div>
            <div class="Add_p_s" id="show1" style="display:none;">
                <label class="form-label col-2">设置积分：</label>

                <div class="formControls col-2"><input type="text" class="input-text" value="" placeholder="积分"
                                                       id="integral"
                ></div>
            </div>
            <div class="Add_p_s" id="show2" style="display:none;">
                <label class="form-label col-2">成本价格：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="成本价格"
                                                       id="costPrice"
                >元
                </div>
            </div>
            <div class="Add_p_s" id="show3" style="display:none;">
                <label class="form-label col-2">零售价格：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="零售价格"
                                                       id="retailPrice"
                >元
                </div>
            </div>
            <div class="Add_p_s">
                <label class="form-label col-2">保质期：</label>

                <div class="formControls col-2"><input type="text" class="input-text" placeholder="保质期"
                                                       id="qualityGuaranteePeriod"
                >年
                </div>
            </div>


        </div>
        <div class="clearfix cl">
            <label class="form-label col-2">备注摘要：</label>
            <div class="formControls col-10">
                <textarea id="remarks" cols="200" rows="20"></textarea>
            </div>
        </div>
        <div class="clearfix cl">
            <label class="form-label col-2">图片上传：</label>
            <input type="hidden" name="picture" id="header" required lay-verify="required">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test1">
                    <i class="layui-icon"></i>上传图片
                </button>
            </div>
            <div class="layui-upload-list">
                <img class="layui-upload-img" style="margin-left: 110px;" height="50px" id="demo1">
                <p id="demoText"></p>
            </div>
        </div>
        <div class="clearfix cl" align="center">
            <div class="Button_operation">

                <input id="btn" type="button" class="btn btn-primary radius" value="确认添加"/>

                <input type="reset" value="重新填写" class="btn btn-success radius"/>

            </div>
        </div>
    </form>
</div>

</div>

<script>

    function change(v) {
        var show1div_display = document.getElementById('show1').style.display;
        var show2div_display = document.getElementById('show2').style.display;
        var show3div_display = document.getElementById('show3').style.display;

        if (v == 1) {
            document.getElementById('show1').style.display = 'none';
            document.getElementById('show2').style.display = 'block';
            document.getElementById('show3').style.display = 'block';
        } else if (v == 2) {
            document.getElementById('show1').style.display = 'block';
            document.getElementById('show2').style.display = 'none';
            document.getElementById('show3').style.display = 'none';
        } else {
            document.getElementById('show1').style.display = 'none';
            document.getElementById('show2').style.display = 'none';
            document.getElementById('show3').style.display = 'none';
        }


    }

    var $ = layui.jquery;
    var upload = layui.upload;

    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/product/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        , done: function (res) {
            if (res.rs) {
                $('#header').val(res.msg);
            }
        }
        , error: function () {

        }
    });


    $(document).ready(function () {
//初始化宽度、高度
        $(".widget-box").height($(window).height());
        $(".page_right_style").height($(window).height());
        $(".page_right_style").width($(window).width() - 220);
        //当文档窗口发生改变时 触发
        $(window).resize(function () {

            $(".widget-box").height($(window).height());
            $(".page_right_style").height($(window).height());
            $(".page_right_style").width($(window).width() - 220);
        });
    });
    $.ajax({
        url: "/product/sort",
        type: "get",
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#sort").append("<option value='" + data[i].id + "'>" + data[i].sortName + "</option>")
            }
        }
    });

    $("#sort").change(function () {
        $("#sorts").html('<option value="0">请选择</option>')
        $("#sortss").html('<option value="0">请选择</option>')
        var id = $("#sort option:selected").val();
        $.ajax({
            url: "/product/sorts?sortId=" + id,
            type: "get",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#sorts").append("<option value='" + data[i].id + "'>" + data[i].sortName + "</option>")
                }
            }
        });
    });

    $("#sorts").change(function () {
        $("#sortss").html('<option value="0">请选择</option>')
        var id = $("#sorts option:selected").val();
        $.ajax({
            url: "/product/sortss?sortId=" + id,
            type: "get",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#sortss").append("<option value='" + data[i].id + "'>" + data[i].sortName + "</option>")
                }
            }
        });
    });

    $("#btn").on("click", function () {
        var re = /^[a-zA-Z0-9]+$/;
        var re1 = /^[0-9]+(.[0-9]{1,2})?$/;
        var re2 = /[u4e00-u9fa5]/;
        var re3 = /^[\u4e00-\u9fa5a-zA-Z]*$/;
        if ($("#productName").val().length == 0) {
            layer.tips('请输入名称!', '#productName', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productName").focus();
        } else if ($("#id").val().length == 0) {
            layer.tips('请输入编号!', '#id', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#id").focus();
        } else if (!re.test($("#id").val())) {
            layer.tips('请输入数字或者字母!', '#id', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#id").focus();
        } else if ($("#productModel").val().length == 0) {
            layer.tips('请输入型号!', '#productModel', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productModel").focus();
        } else if (!re.test($("#productModel").val())) {
            layer.tips('请输入数字或者字母!', '#productModel', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productModel").focus();
        } else if ($("#producingArea").val().length == 0) {
            layer.tips('请输入产地!', '#producingArea', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#producingArea").focus();
        } else if (!re3.test($("#producingArea").val())) {
            layer.tips('请输入中文或英文!', '#producingArea', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#producingArea").focus();
        } else if ($("#materialQuality").val().length == 0) {
            layer.tips('请输入材质!', '#materialQuality', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#materialQuality").focus();
        } else if ($("#brand").val().length == 0) {
            layer.tips('请输入品牌!', '#brand', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#brand").focus();
        } else if ($("#productQuality").val().length == 0) {
            layer.tips('请输入质量!', '#productQuality', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productQuality").focus();
        } else if (!re1.test($("#productQuality").val())) {
            layer.tips('请输入正确的数字!', '#productQuality', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productQuality").focus();
        } else if ($("#productQuality").val() == 0) {
            layer.tips('请输入正确的数字!', '#productQuality', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productQuality").focus();
        } else if ($("#sort").val() == 0) {
            layer.tips('请输入分类!', '#sort', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#sort").focus();
        } else if ($("#sorts").val() == 0) {
            layer.tips('请输入分类!', '#sorts', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#sorts").focus();
        } else if ($("#sortss").val() == 0) {
            layer.tips('请输入分类!', '#sortss', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#sortss").focus();
        } else if ($("#productType").val() == 0) {
            layer.tips('请输入种类!', '#productType', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#productType").focus();
        } else if ($("#productType").val() == 2 && $("#integral").val().length == 0) {
            layer.tips('请输入积分!', '#integral', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#integral").focus();
        } else if ($("#productType").val() == 2 && !re1.test($("#integral").val())) {
            layer.tips('请输入正确的数字!', '#integral', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#integral").focus();
        } else if ($("#productType").val() == 2 && $("#integral").val() == 0) {
            layer.tips('请输入正确的数字!', '#integral', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#integral").focus();
        } else if ($("#productType").val() == 1 && $("#costPrice").val().length == 0) {
            layer.tips('请输入成本!', '#costPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#costPrice").focus();
        } else if ($("#productType").val() == 1 && !re1.test($("#costPrice").val())) {
            layer.tips('请输入正确的数字!', '#costPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#costPrice").focus();
        } else if ($("#productType").val() == 1 && $("#costPrice").val() == 0) {
            layer.tips('请输入正确的数字!', '#costPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#costPrice").focus();
        } else if ($("#productType").val() == 1 && $("#retailPrice").val().length == 0) {
            layer.tips('请输入零售价!', '#retailPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#retailPrice").focus();
        } else if ($("#productType").val() == 1 && !re1.test($("#retailPrice").val())) {
            layer.tips('请输入正确的数字!', '#retailPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#retailPrice").focus();
        } else if ($("#productType").val() == 1 && $("#retailPrice").val() == 0) {
            layer.tips('请输入正确的数字!', '#retailPrice', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#retailPrice").focus();
        } else if ($("#qualityGuaranteePeriod").val().length == 0) {
            layer.tips('请输入保质期!', '#qualityGuaranteePeriod', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#qualityGuaranteePeriod").focus();
        } else if (!re1.test($("#qualityGuaranteePeriod").val())) {
            layer.tips('请输入正确的数字!', '#qualityGuaranteePeriod', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#qualityGuaranteePeriod").focus();
        } else if ($("#qualityGuaranteePeriod").val() == 0) {
            layer.tips('请输入正确的数字!', '#qualityGuaranteePeriod', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#qualityGuaranteePeriod").focus();
        } else if ($("#remarks").val().length == 0) {
            layer.tips('请输入备注!', '#remarks', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#remarks").focus();
        } else if ($("#demo1").attr("src") == null) {
            layer.tips('请上传图片!', '#test1', {
                tips: [2, 'red'],
                time: 2000
            });
            $("#test1").focus();
        } else {
            if ($("#productType").val() == 2) {
                $("#costPrice").val("");
                $("#retailPrice").val("");
            } else if ($("#productType").val() == 1) {
                $("#integral").val("");
            }
            $.ajax({
                url: '/product/productByIdss',
                data: {
                    "id": $("#id").val(),
                },
                contentType: 'application/json',           //表示我们发送给服务器数据是json格式
                dataType: 'json',                          //表示服务器返回给我们的数据是json格式
                success: function (data) {
                    if (data.result == true) {
                        layer.tips('该商品已存在!', '#id', {
                            tips: [2, 'red'],
                            time: 2000
                        });
                        $("#id").focus();
                    } else {
                        $.ajax({
                            url: '/product/addProduct',
                            data: {
                                "id": $("#id").val(),
                                "productName": $("#productName").val(),
                                "productModel": $("#productModel").val(),
                                "producingArea": $("#producingArea").val(),
                                "picture": $("#header").val(),
                                "materialQuality": $("#materialQuality").val(),
                                "brand": $("#brand").val(),
                                "productQuality": $("#productQuality").val(),
                                "sortId": $("#sort").val(),
                                "sortsId": $("#sorts").val(),
                                "sortssId": $("#sortss").val(),
                                "productType": $("#productType").val(),
                                "integral": $("#integral").val(),
                                "costPrice": $("#costPrice").val(),
                                "retailPrice": $("#retailPrice").val(),
                                "qualityGuaranteePeriod": $("#qualityGuaranteePeriod").val(),
                                "remarks": $("#remarks").val(),
                            },
                            contentType: 'application/json',           //表示我们发送给服务器数据是json格式
                            dataType: 'json',                          //表示服务器返回给我们的数据是json格式
                            success: function (data) {
                                if (data.result == true) {
                                    layer.msg('添加成功！', {icon: 1, time: 1000}, function () {
                                        window.location.href = "/product/products_info";
                                    });
                                } else {
                                    layer.msg('添加失败！', {icon: 1, time: 1000}, function () {
                                        window.location.href = "/product/products_info";
                                    });
                                }
                            }
                        })
                    }
                }
            })
        }
    })


</script>
</body>
</html>